﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;

public class ReconnectPanel : BasePopupPanel
{
    private GTextField tfTitle;
    private int time;
    public override void Init()
    {
        base.Init();

        frame = view.GetChild("n1").asCom;

        frame.GetChild("n3").asButton.onClick.Add(OnCancelClick);
        tfTitle = frame.GetChild("n2").asTextField;
    }

    public override void Show(params object[] para)
    {
        base.Show(para);
        time = 1;
        tfTitle.SetVar("time",time.ToString());
        tfTitle.FlushVars();
        Timers.inst.Add(1, 0, OnConnectCall);
    }

    private void OnConnectCall(object param)
    {
        if (GameNetwork.IsConnected)
        {
            Close();
            return;
        }

        ++time;
        tfTitle.SetVar("time", time.ToString());
        tfTitle.FlushVars();

        if (time % 2 == 0){
            GameNetwork.Connect();
        }
    }

    public override void Close()
    {
        Timers.inst.Remove(OnConnectCall);
        base.Close();
    }

    protected override GComponent CreateView()
    {
        return UIPackage.CreateObject("Main", "panel_reconnnect1").asCom;
    }

    protected override bool isClickClose()
    {
        return false;
    }

    private void OnCancelClick(EventContext context)
    {
        Close();
        PanelManager.Open<LoginPanel>();
    }
}
